/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package cn.edu.just.secondwebapp.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
 *
 * @author JUST
 */
public class DatabaseConnectionUtil {
    public static final String DBDRIVER = "com.mysql.jdbc.Driver";
    public static final String DBURL  = "jdbc:mysql://dusin.top:3306/jsp?useUnicode=true&characterEncoding=utf8";
    public static final String USERNAME = "jsp";
    public static final String PASSWORD = "jsp123";

    Connection conn = null;
    
    public DatabaseConnectionUtil() {
        try {
            Class.forName(DBDRIVER);
            conn = DriverManager.getConnection(DBURL, USERNAME, PASSWORD);
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    
    public Connection getConnection(){
        if(conn==null){
            try {
                Class.forName(DBDRIVER);
                conn = DriverManager.getConnection(DBURL, USERNAME, PASSWORD);
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
        return conn;
    }
    
    public void close(){
        
        try {
            conn.close();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
    
}
